package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbOrderItemService;
import com.dongdongshop.vo.CartVo;
import com.dongdongshop.vo.TbAddressVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("skip")
public class Skip {

    //获取登录账户信息
    @RequestMapping("user")
    @ResponseBody
    public TbUser user() {
        TbUser tbUser = (TbUser)SecurityUtils.getSubject().getPrincipal();
        return tbUser;
    }

    //注入service
    @DubboReference(timeout = 10000)
    private TbOrderItemService tbOrderItemService;
    @Autowired
    private CartController controller;
    @Autowired
    private TbAddressVoController tbAddressVoController;
    @Autowired
    private AlipayController alipayController;

    //跳转到登录页面
    @RequestMapping("tologin")
    public String tologin() {
        return "login";
    }

    //跳转到支付成功页面
    @RequestMapping("topaysuccess")
    public String topaysuccess() {
        return "paysuccess";
    }

    //跳转到代发货页面成功页面
    @RequestMapping("tohomeOrderSend")
    public String tohomeOrderSend() {
        return "home-order-send";
    }

    //跳转到支付失败页面
    @RequestMapping("topayfail")
    public String topayfail() {
        return "payfail";
    }

    //跳转到支付单号首页页面
    @RequestMapping("toalipayindex")
    public String toalipayindex() {
        return "alipay/index";
    }

    //跳转到注册页面
    @RequestMapping("toregister")
    public String toregister() {
        return "register";
    }

    //跳转到主页面
    @RequestMapping("toindex")
    public String toindex() {
        return "index";
    }

    //跳转到购物车页面
    @RequestMapping("tocart")
    public String tocart() {
        return "cart";
    }

    //跳转到我的东东商城
    @RequestMapping("tohomeindex")
    public String tohomeindex() {
        return "home-index";
    }

    //跳转到地址管理
    @RequestMapping("tohomejsettingaddress")
    public String tohomejsettingaddress() {
        return "home-setting-address";
    }

    //商品已成功加入购物车
    @RequestMapping("tosuccesscart")
    public String tosuccesscart() {
        return "success-cart";
    }

    //跳转到秒杀页面
    @RequestMapping("toseckillindex")
    public String toseckillindex() {
        return "seckill-index";
    }

    //跳转到合作招商页面
    @RequestMapping("tocooperation")
    public String tocooperation() {
        return "cooperation";
    }

    //跳转到结算页面
    @RequestMapping("togetOrderInfo")
    public String togetOrderInfo(Long[] ids, HttpServletRequest request, HttpServletResponse response, Model model) {
        //根据购物车中id值去redis中获取
        Result<List<CartVo>> listResult = controller.getcartList(request, response);
        List<CartVo> list1 = new ArrayList<>();
        for (int i = 0; i < listResult.getData().size(); i++) {
            List<TbOrderItem> cartDatailList = listResult.getData().get(i).getCartDatailList();
            CartVo cartVo = new CartVo();
            cartVo.setSellerName(listResult.getData().get(i).getSellerName());
            List<TbOrderItem> list = new ArrayList<>();
            for (int j = 0; j < cartDatailList.size(); j++) {
                Long itemId = cartDatailList.get(j).getItemId();
                if (Arrays.asList(ids).contains(itemId)) {
                    list.add(cartDatailList.get(j));
                }
            }
            cartVo.setCartDatailList(list);
            list1.add(cartVo);
        }
        model.addAttribute("shuju", list1);
        request.getSession().setAttribute("a", list1);
        return "getOrderInfo";
    }

    //跳转到支付页面
    @RequestMapping("topay")
    public String topay(HttpServletRequest request, Long id, BigDecimal payment,Model model) {
        List<CartVo> a = (List<CartVo>) request.getSession().getAttribute("a");
        List<TbAddressVo> result = (List<TbAddressVo>) tbAddressVoController.showTbAddressVo().getData();
        List<TbOrderItem> list = new ArrayList<>();
        for (int i = 0; i < a.size(); i++) {
            for (int j = 0; j < a.get(i).getCartDatailList().size(); j++) {
                list.add(a.get(i).getCartDatailList().get(j));
            }
        }
        TbAddressVo vo = null;
        for (int i = 0; i < result.size(); i++) {
            if (result.get(i).getId() == id) {
                vo = result.get(i);
            }
        }

        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //将订单从redis删除,平且存入数据库
        String add = tbOrderItemService.add(vo, list, payment, tbUser.getUsername(), tbUser.getId());
        model.addAttribute("WIDout_trade_no",add);
        model.addAttribute("WIDtotal_amount",payment.toString());
        model.addAttribute("WIDsubject","东东商城收款方");
        model.addAttribute("WIDbody","官方正品");
        return "pay";
    }

}
